PasswordGenerator.tsx ➔ PasswordGenerator   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 21
c 0
b 0
f 0
rs 9.45
cc 2
1
import { useState } from "react";
2
3
export default function PasswordGenerator() {
4
  const [length, setLength] = useState(12);
5
  const [result, setResult] = useState("");
6
7
  const generatePassword = () => {
8
    const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%";
9
    let password = "";
10
    for (let i = 0; i < length; i++) {
11
      password += charset.charAt(Math.floor(Math.random() * charset.length));
12
    }
13
    setResult(password);
14
  };
15
16
  return (
17
    <div>
18
      <h2>Password Generator</h2>
19
      <input type="number" value={length} onChange={(e) => setLength(Number(e.target.value))} min={4} max={50} />
20
      <button onClick={generatePassword}>Generate</button>
21
      <input type="text" value={result} readOnly />
22
    </div>
23
  );
24
}